package com.example.ywj.toutiao.utils;

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.Item;

import java.util.List;

public class My_BaseAdapter extends BaseAdapter {
    private List<Item> itemList;
    public My_BaseAdapter(List<Item> itemList){
        this.itemList = itemList;

    }
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //int currentType = getItemViewType(position);
        ViewHolder viewHolder ;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.item_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.tv_title = convertView.findViewById(R.id.item_title);
            viewHolder.tv_extra = convertView.findViewById(R.id.item_extra);
            viewHolder.tv_go = convertView.findViewById(R.id.item_go);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tv_title.setText(itemList.get(position).getTitle());
        viewHolder.tv_extra.setText(itemList.get(position).getExtra());
        Typeface font = Typeface.createFromAsset(convertView.getContext().getAssets(), "iconfont.ttf");
        viewHolder.tv_go.setTypeface(font);
        viewHolder.tv_go.setText(convertView.getContext().getResources().getString(R.string.go));
        return convertView;
    }
    class ViewHolder{
        TextView tv_title;
        TextView tv_extra;
        TextView tv_go;
    }
}
